﻿namespace ScrumFactory.Data.Sql.Tests
{
    using System.Configuration;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ScrumFactory.Data.Sql;
    using System.Transactions;

    [TestClass()]
    public class SqlProjectsRepositoryTest
    {
        [TestMethod()]
        public void GetsAllProjects()
        {
            var target = new SqlProjectsRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

            var result = target.GetAllProjects();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.Any(p => p.Id == "project1" && p.Name == "Project 1"));
        }

        [TestMethod()]
        public void GetProjectReturnsNull()
        {
            var target = new SqlProjectsRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

            var result = target.GetProject("nonExistentProject");

            Assert.IsNull(result);
        }

        [TestMethod()]
        public void GetProjectReturnsProject()
        {
            var target = new SqlProjectsRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

            var result = target.GetProject("project1");

            Assert.IsNotNull(result);
            Assert.AreEqual("project1", result.Id);
            Assert.AreEqual("Project 1", result.Name);
        }

        [TestMethod()]
        public void UpdateProjectUpdatesProject()
        {
            using (var transaction = new TransactionScope())
            {
                var target = new SqlProjectsRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

                target.UpdateProject(new ScrumFactory.Project { Id = "project1", Name = "New name" });

                var result = target.GetProject("project1");

                Assert.IsNotNull(result);
                Assert.AreEqual("New name", result.Name);
            }
        }

        [TestMethod()]
        public void InsertProjectInsertsProject()
        {
            using (var transaction = new TransactionScope())
            {
                var target = new SqlProjectsRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

                target.InsertProject(new ScrumFactory.Project { Id = "newProject", Name = "New project" });

                var result = target.GetProject("newProject");

                Assert.IsNotNull(result);
                Assert.AreEqual("newProject", result.Id);
                Assert.AreEqual("New project", result.Name);
            }
        }
    }
}
